package com.wangxiang.shop.order.controller;

import java.util.HashMap;
import java.util.List;


import com.wangxiang.common.utils.PageEntity;
import com.wangxiang.common.utils.R;
import com.wangxiang.shop.order.entity.DevOrderEntity;
import com.wangxiang.shop.order.entity.MemberOrderEntity;
import com.wangxiang.shop.order.entity.ShopCartEntity;
import com.wangxiang.shop.order.feign.MemberFeign;
import com.wangxiang.shop.order.feign.ProductFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.wangxiang.shop.order.service.MemberOrderService;


/**
 * 
 *
 * @author wangxiang
 * @email 2293672863@qq.com
 * @date 2022-04-08 15:29:08
 */
@RestController
@RequestMapping("order")
public class MemberOrderController {
    @Autowired
    private MemberOrderService memberOrderService;
    @Autowired
    ProductFeign productFeign;
    @Autowired
    MemberFeign memberFeign;


    /**
     * 远程接口调用
     * @param list
     * @return
     */
    @RequestMapping("add")
    public List<Integer> addByCart(@RequestBody List<ShopCartEntity> list){
        List<Integer> cartIdList = memberOrderService.addByCart(list);
        return cartIdList;
    }
    @RequestMapping("page")
    public R page(@RequestBody PageEntity page){
        PageEntity list = memberOrderService.page(page);
        List<HashMap<String, Object>> pageList = memberOrderService.pageList((List<MemberOrderEntity>) list.getList());
        List<HashMap<String, Object>> memberOrderList = memberFeign.memberOrderList(pageList);
        List<HashMap<String, Object>> productMemberOrderList = productFeign.memberOrderList(memberOrderList);
        return R.ok().put("page",list).put("list",productMemberOrderList);
    }
    @RequestMapping("list")
    public R list(@RequestBody HashMap<String , Integer> map){
        //统一参数 0 全部 1代发货 2待收货 3待评价，历史订单
//        System.out.println("数据"+map);
        int userId = map.get("userId");
        Integer typeId = map.get("typeId");
        List<HashMap<String, Integer>> list = memberOrderService.list(userId, typeId);
//        System.out.println("返回数据"+list);
        if (list==null){
            return null;
        }
        List<HashMap<String, String>> order = productFeign.order(list);
        List<DevOrderEntity> devOrderEntities = memberOrderService.orderList(order);

        return R.ok().put("data",devOrderEntities);
    }
    @RequestMapping("getTypeNum")
    public R getTypeNumByUserId(@RequestBody HashMap<String,Integer> map){
//        System.out.println("map"+map);
        int userId = Integer.valueOf(map.get("userId"));
        HashMap<String, Integer> typeNumList = memberOrderService.typeNumList(userId);
//        System.out.println("typeNumList"+typeNumList);
        return R.ok().put("data",typeNumList);

    }

    /**
     * 退订，退单
     * @param devOrder
     * @return
     */
    @RequestMapping("outgoods")
    public R outGoods(@RequestBody DevOrderEntity devOrder){
        int i = memberOrderService.outGoods(devOrder);
        if (i==0){
            return R.error("退订申请失败");
        }
        return R.ok().put("msg","你已申请退订");
    }
    @RequestMapping("addByPayNow")
    public int addByPayNow(@RequestBody HashMap<String ,Object> map){
//        System.out.println("立即购买");
        int i = memberOrderService.addByPayNow(map);
//        System.out.println(map);
        return i;
    }

    @RequestMapping("receiptGoods")
    public R receiptGoods(@RequestBody HashMap<String , Integer> map){
        int i = memberOrderService.receiptGoods(map);
        if (i==0){
            return R.error("确定收货操作失败，系统错误");
        }
        return R.ok().put("msg","收货成功");
    }
    @RequestMapping("audit")
    public R audit(@RequestBody HashMap<String , Object> map){
        int status = Integer.valueOf(map.get("status").toString());
       String str = "拒绝操作必须选择理由";
        if (status == 1){
            if (map.get("remark")==null){
                return R.error("拒绝操作必须选择理由");
            }
            if (map.get("remark")!=null){
                String remark = map.get("remark").toString();
                if (str.equals(remark)){
                    return R.error("拒绝操作必须选择理由");
                }
            }

        }
        int i = memberOrderService.auditUnsubscribeApply(map);
        if (i==0){
            return R.error("操作失败，请手动更新数据库");
        }
        return R.ok().put("msg","操作成功").put("code",800);
    }
    @RequestMapping("search")
    public R search(@RequestBody HashMap<String ,Object> map){
        String strNull = "";
        if (strNull.equals(map.get("userName"))|map.get("userName")==null){
            return R.error("表单校验失败，请输入在查询");
        }
        String replace = map.get("userName").toString().replace(" ", "");
        HashMap<String, Object> stringObjectHashMap = new HashMap<>();
        stringObjectHashMap.put("userName",replace);
        int r = memberFeign.orderSearch(stringObjectHashMap);
        if (r==0){
            return R.error("用户不存在");
        }
        PageEntity pageEntity = memberOrderService.pageByUser(map, r);
        List<HashMap<String, Object>> list = memberOrderService.pageList((List<MemberOrderEntity>) pageEntity.getList());
        List<HashMap<String, Object>> memberOrderList = memberFeign.memberOrderList(list);
        List<HashMap<String, Object>> productMemberOrderList = productFeign.memberOrderList(memberOrderList);
        if (productMemberOrderList.size()==0) {
            return R.ok().put("msg","用户没有订购商品").put("code",700);
        }
        return R.ok().put("page",pageEntity).put("list",productMemberOrderList);

    }


}
